/**
 * @author John Hewison
 * @author $LastChangedBy: john.hewison $:  $ $LastChangedDate: 2009-02-01 11:45:01 +0000 (Sun, 01 Feb 2009) $:
 * @version $Revision: 43 $:
 */
package dk.hewison.client;

import com.google.gwt.core.client.GWT;
import dk.hewison.client.component.LogDlg;

public class Log {
    private static Log ourInstance = new Log();

    private StringBuffer buf;
    private static LogDlg logDlg;

    public static void debug(String s) {
        getInstance().doDebug(s);
    }

    public static void showLog() {
        getInstance().doShowLog();
    }

    private void doShowLog() {
        if (logDlg == null) {
            logDlg = new LogDlg();
        }
        logDlg.setLogText(buf);
        logDlg.center();
    }

    private void doDebug(String s) {
        if (!GWT.isScript()) {
            System.out.println(s);
        }
        buf.append(s).append("\n");
        appendMessage(s);
    }

    private void appendMessage(String s) {
        if (logDlg != null && logDlg.isVisible()) {
            logDlg.appendMessage(s);
        }
    }

    public static Log getInstance() {
        return ourInstance;
    }

    private Log() {
        buf = new StringBuffer(2048);
    }

    public static void error(String s) {
        getInstance().doDebug(s);
    }
}
